package cn.mrlong.base.ktnet

import cn.mrlong.base.ktnet.baseconfig.BaseResponse
import cn.mrlong.base.ktnet.err.FactoryException
import cn.mrlong.base.ktnet.err.ServerException
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

/**
 * 作者：BINGO
 * 时间：2021/03/26 15:41
 * 使用Rxjava 执行网络请求
 */
object Net {
    fun <T> request(observable: Observable<BaseResponse<T>>, callBack: HttpCallBack<T>) {
        RetrofitManager.get()
                .subscribe(
                        observable,
                        object : Observer<BaseResponse<T>> {
                            override fun onSubscribe(d: Disposable) {
                            }

                            override fun onNext(t: BaseResponse<T>) {
                                if (t.isSuccess()) {
                                    callBack.success(t.data)
                                } else {
                                    callBack.error(
                                            FactoryException.analysisExcetpion(
                                                    ServerException(
                                                            if (null == t.message) FactoryException.SERVER_MSG else t.message!!
                                                    )
                                            )
                                    )
                                }
                            }

                            override fun onError(e: Throwable) {
                                callBack.error(
                                        FactoryException.analysisExcetpion(e)
                                )
                            }

                            override fun onComplete() {
                                //callBack.complete()
                            }
                        })

    }
}